package main

import (
	"encoding/hex"
	"fmt"
	"gitlab.local/backend/proto/dto"
	"signal/bll"
	"signal/model"
	"strings"
)

func main() {
	//bytesToObject()
	buildPacket()
}

func bytesToObject() {
	items := []struct {
		str  string
		want interface{}
	}{
		{
			str: "a55a16003321edeed905884b01026c5a000000040000cebc21c1",
			want: &bll.PacketBSC22{
				Magic:       0x5aa5,
				Len:         22,
				ID:          5442606583251804467,
				DeviceType:  1,
				Cmd:         0x02,
				BindAddress: 0x5a6c,
				BindLength:  1024,
				Body:        []byte{},
				Crc32:       0xc121bcce,
			},
		},
		{
			str: "a55a1a003321edeed905884b010100000000d894000002000000801fdf3a",
			want: &bll.PacketBSC22{
				Magic:       0x5aa5,
				Len:         26,
				ID:          5442606583251804467,
				DeviceType:  1,
				Cmd:         0x01,
				BindAddress: 0x0,
				BindLength:  38104,
				Body:        []byte{0x02, 0x00, 0x00, 0x00},
				Crc32:       0x3adf1f80,
			},
		},
		{
			str: "FE1E0082C8D23966406C8415406C841500000000002003000000000104000000",
			want: &model.Packet{
				Magic: 0xaa,
				Len:   0x1e,
				ID:    0x00,
				Cmd:   0x82,
				Body: func() []byte {
					b, _ := hex.DecodeString("C8D23966406C8415406C841500000000002003000000000104000000")
					return b
				}(),
			},
		},
	}

	for _, item := range items {
		if v, ok := item.want.(*bll.PacketBSC22); ok {
			b, _ := hex.DecodeString(item.str)
			fmt.Println(fmt.Sprintf("%X", b))
			obj := bll.BytesToObject(b)
			if obj != nil {
				result := obj.(*bll.PacketBSC22)
				if bb, err := bll.Compare(result, v); !bb {
					fmt.Println("str:", item.str, " 错误:", item.want, ",err:", err)
				}
			}
		}
		if v, ok := item.want.(*model.Packet); ok {
			b, _ := hex.DecodeString(item.str)
			fmt.Println(fmt.Sprintf("%X", b))
			obj := model.BytesToObject(b)
			if obj != nil {
				result := obj.(*model.Packet)
				if bb, err := model.Compare(result, v); !bb {
					fmt.Println("str:", item.str, " 错误:", item.want, ",err:", err)
				}
			}
		}
	}
}

func buildPacket() {
	var (
		bsc22es = []struct {
			*dto.UpgradeBSC22Data
			want string
		}{
			{
				UpgradeBSC22Data: &dto.UpgradeBSC22Data{
					UpgradeCmd:  dto.BSC22UpgradeCmdType_Cmd0x01,
					Id:          uint64(12731161927571471055),
					Sn:          "BSC2100013500047",
					DeviceType:  1,
					BindLength:  85544,
					BindAddress: 0,
					Body:        []byte{0x00, 0x00, 0x00, 0x00},
				},
				want: "a55a1a00cffadf0e522caeb0010100000000284e01000000000038786036",
			},
			{
				UpgradeBSC22Data: &dto.UpgradeBSC22Data{
					UpgradeCmd:  dto.BSC22UpgradeCmdType_Cmd0x00,
					Id:          uint64(12731161927571471055),
					Sn:          "BSC2100013500047",
					DeviceType:  1,
					BindLength:  0,
					BindAddress: 0,
					Body:        []byte{0x81, 0x00, 0x00, 0x00},
				},
				want: "a55a1a00cffadf0e522caeb0010000000000000000008100000081d4e84b",
			},
			{
				UpgradeBSC22Data: &dto.UpgradeBSC22Data{
					UpgradeCmd:  dto.BSC22UpgradeCmdType_Cmd0x02,
					Id:          uint64(12731161927571471055),
					Sn:          "BSC2100013500047",
					DeviceType:  1,
					BindLength:  1024,
					BindAddress: 0,
					Body:        nil,
				},
				want: "a55a1600cffadf0e522caeb00102000000000004000081496420",
			},
		}
	)

	for _, bsc22 := range bsc22es {
		got := strings.ToLower(fmt.Sprintf("%X", bll.BuildPacket(bsc22.UpgradeBSC22Data).ToHexBytes()))
		if bsc22.want == got {
			fmt.Println("match")
		} else {
			fmt.Println("not match:", "got:", got, "want:", bsc22.want)
		}
	}
}
